/**
 * 
 * JShakki  - Dude with recycled paper.
 * 
 * @version 1.0
 * 
 * Copyright (C) 2006 Jarkko Sipilä
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * 
 */

package dude.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.WindowConstants;

import dude.data.Board;
import dude.data.Dude;
import dude.data.DudeDrawable;


public class DudeComponent extends JFrame implements DudeDrawable {
  
  private static final long serialVersionUID = 1L;
  
  private Dude js;
  
  private JLabel maxLabel;
  private JSlider sizeOfBoardSlider;
  private JLabel sizeOfBoardLabel;
  private BoardComponent board;
  private JLabel minLabel;
  private JButton newGameButton;
  
  
  public DudeComponent( int size ) {
    this.js = new Dude( this, size );    
    this.initComponents(size);
    System.gc();
  }
  
  
  public void drawLine( Dimension d, int suunta ) {
    this.board.drawLine( d, suunta );
  }
  
  public void drawX( Dimension d ) {
    this.board.drawX( d );
  }
  
  public void drawO( Dimension d ) {
    this.board.drawO( d );
  }
  
  
  
  protected void dataSetOwner( Dimension d ) {
    this.js.dataSetOwner( d );
  }
  
  private void initComponents( int boardSize ) {
    GridBagConstraints gridBagConstraints;
    
    board = new BoardComponent( boardSize, this );
    sizeOfBoardSlider = new JSlider();
    sizeOfBoardLabel = new JLabel();
    minLabel = new JLabel();
    maxLabel = new JLabel();
    newGameButton = new JButton();
    
    this.getContentPane().removeAll();
    this.getContentPane().setLayout(new GridBagLayout());
    
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
    board.setMinimumSize( new Dimension(800, 800) );
    board.setPreferredSize( new Dimension(800, 800) );
    board.updateUI();
    
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.insets = new Insets( 20, 0, 0, 0 );
    gridBagConstraints.anchor = GridBagConstraints.NORTH;
    getContentPane().add(board, gridBagConstraints);
    
    sizeOfBoardSlider.setMinimum(Board.MINIMUM_SIZE);
    sizeOfBoardSlider.setMaximum(Board.MAXIMUM_SIZE);
    sizeOfBoardSlider.setValue( boardSize );
    sizeOfBoardSlider.setMajorTickSpacing(1);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    getContentPane().add( sizeOfBoardSlider, gridBagConstraints );
    
    sizeOfBoardLabel.setText("Size");
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.insets = new Insets(20, 0, 6, 0);
    getContentPane().add(sizeOfBoardLabel, gridBagConstraints);
    
    minLabel.setText("" + Board.MINIMUM_SIZE);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.insets = new Insets(5, 300, 0, 0);
    getContentPane().add(minLabel, gridBagConstraints);
    
    maxLabel.setText("" + Board.MAXIMUM_SIZE);
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.insets = new Insets(0, 5, 0, 300);
    getContentPane().add(maxLabel, gridBagConstraints);
    
    newGameButton.setText("New Game");
    newGameButton.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent evt) {
        newGameButtonMouseClicked(evt);
      }
    });
    
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = GridBagConstraints.SOUTH;
    gridBagConstraints.insets = new Insets(17, 0, 5, 0);
    getContentPane().add(newGameButton, gridBagConstraints);
    
    pack();
  }
  
  private void newGameButtonMouseClicked( MouseEvent evt ) {
    int koko = this.sizeOfBoardSlider.getValue();
    this.js.newGame( koko );
    this.initComponents(koko);
  }
}
